layui.use('layer', function () {
    var layer = layui.layer;
})
/*封装Color*/
function color() {
    var color = "rgb(" + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + ")";
    return color;
}
var code; //在全局定义验证码   
//产生验证码  
function createCode() {
    code = "";
    var codeLength = 4;//验证码的长度  
    var checkCode = document.getElementById("code");
    var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
        'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');//随机数  
    for (var i = 0; i < codeLength; i++) {//循环操作  
        var index = Math.floor(Math.random() * 36);//取得随机数的索引（0~35）  
        code += random[index];//根据索引取得随机数加到code上  
    }
    checkCode.value = code;//把code值赋给验证码  
    checkCode.style.color = color();
}
//校验验证码  
function validate() {
    var num = 0;
    var str = "";
    $("input[type$='text'],input[type$='password']").each(function (n) {
        if ($(this).val() == "") {

            layui.layer.alert(str += "" + $(this).attr("data-name") + "不能为空!", {
                title: '提示框',
                icon: 0,
            });
            num++;
            return false;
        }
    });
    var inputCode = document.getElementById("authcode").value.toUpperCase(); //取得输入的验证码并转化为大写                
    if (num > 0) {
        return false;
    } else if (inputCode != code) {
        layui.layer.alert("验证码输入错误！@_ @", {
            title: '提示框',
            icon: 0,
        });

        createCode();//刷新验证码  
        document.getElementById("authcode").value = "";//清空文本框  
        return false;
    }
    else {
        $.ajax({
            type: "post",
            url: "/Login/UserLogin",
            data: { UserName: $("#loginname").val(), Pwd: $("#loginpwd").val() },
            async: false,
            success: function (data) {
                if (data.UserName != "") {
                    var index = layer.load(3, { shade: 0.1 })
                    setTimeout(function () {
                        layer.msg("登录成功", { icon: 1 });
                        layer.close(index);
                        window.location.href = "/Home/Index";
                    }, 2000);
                } else {
                    layer.alert('账号或密码错误！', {
                        title: '提示框',
                        icon: 1,
                    });
                }
            }
        })
    }
}  